﻿
//jquery.pager 分页器插件

$.fn.extend({
    //一般处理程序--分页器
    "pager": function (url, args, callback) {
        //添加四个按钮，以及一个label标签
        this.css("text-align", "right").css("padding-right", "30px");
        this.append(' <label>共有120条记录，当前第1/10页</label>')
            .append('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;')
            .append('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;')
            .append('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;')
            .append(' <input type="image" value="首页" src="../images/first.gif" />&nbsp;&nbsp;')
            .append(' <input type="image" value="上一页" src="../images/back.gif" />&nbsp;&nbsp;')
            .append(' <input type="image" value="下一页" src="../images/next.gif" />&nbsp;&nbsp;')
            .append(' <input type="image" value="尾页" src="../images/last.gif" />&nbsp;&nbsp;')
            .append(' 转到第<input type="text" size="1" /><input type="image" src="../images/go.gif" onclick=tiao(); />')
          
        //初始化三个本插件中的全局变量
        var _this = this;
        this.attr("pageIndex", 1);
        this.attr("pageSize", 3);
        this.attr("pageCount", 1);

        //重新加载数据(私有函数)
        var reloadList = function () {
            //得到三个参数
            var pageIndex = _this.attr("pageIndex");
            var pageSize = _this.attr("pageSize");
            var pageCount = _this.attr("pageCount");
            //修正页码
            if (parseInt(pageIndex) < 1)
                pageIndex = 1;
            if (parseInt(pageIndex) > parseInt(pageCount))
                pageIndex = pageCount;
            //对参数进行处理
            args = args || {};
            args.pageIndex = pageIndex;
            args.pageSize = pageSize;
            _this.attr("pageIndex", pageIndex);
            _this.attr("pageSize", pageSize);
            //请求数据
            $.get(url, args, function (result) {
                //将返回的结果转为js对象
                var r = JSON.parse(result);
                //求出总页数并显示
                pageCount = parseInt((r.total - 1) / pageSize) + 1;
                count = parseInt(r.total);
                _this.attr("pageCount", pageCount);
                _this.find("label").text("共有" + count + "条记录, " + "当前第" + pageIndex + "/" + pageCount + "页");
                //调用回调函数
                callback(r.data);
            });
        }
        //进行首次请求
        reloadList();

        //为四个按钮编写单击事件
        _this.find(":image:eq(0)").click(function () {
            _this.attr("pageIndex", 1);
            reloadList();
        });
        _this.find(":image:eq(1)").click(function () {
            var pageIndex = _this.attr("pageIndex");
            pageIndex--;
            _this.attr("pageIndex", pageIndex);
            reloadList();
        });
        _this.find(":image:eq(2)").click(function () {
            var pageIndex = _this.attr("pageIndex");
            pageIndex++;
            _this.attr("pageIndex", pageIndex);
            reloadList();
        });
        _this.find(":image:eq(3)").click(function () {
            var pageCount = _this.attr("pageCount");
            _this.attr("pageIndex", pageCount);
            reloadList();
        });
    },


    //WebService版分页器
    "pager2": function (url, args, callback) {
        //添加四个按钮，以及一个label标签
        this.css("text-align", "right").css("padding-right", "30px");
        this.append(' <label>共有120条记录，当前第1/10页&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>')
            .append(' <input type="image" value="首页" src="../images/first.gif" />&nbsp;&nbsp;')
            .append(' <input type="image" value="上一页" src="../images/back.gif" />&nbsp;&nbsp;')
            .append(' <input type="image" value="下一页" src="../images/next.gif" />&nbsp;&nbsp;')
            .append(' <input type="image" value="尾页" src="../images/last.gif" />&nbsp;&nbsp;')
            .append(' 转到第<input type="text" size="1" /><input type="image" src="../images/go.gif" onclick=tiao(); />')
        //初始化三个本插件中的全局变量
        var _this = this;
        this.attr("pageIndex", 1);
        this.attr("pageSize", 3);
        this.attr("pageCount", 1);

        //重新加载数据(私有函数)
        var reloadList = function () {
            //得到三个参数
            var pageIndex = _this.attr("pageIndex");
            var pageSize = _this.attr("pageSize");
            var pageCount = _this.attr("pageCount");
            //修正页码
            if (parseInt(pageIndex) < 1)
                pageIndex = 1;
            if (parseInt(pageIndex) > parseInt(pageCount))
                pageIndex = pageCount;
            //对参数进行处理
            args = args || {};
            args.pageIndex = pageIndex;
            args.pageSize = pageSize;
            _this.attr("pageIndex", pageIndex);
            _this.attr("pageSize", pageSize);
            //请求数据
            $.ajax({
                url: url,
                type: "post",
                data: JSON.stringify(args),
                dataType: "json",
                contentType: "application/json",
                success: function (obj) {
                    var r = obj.d;
                    //求出总页数并显示
                    pageCount = parseInt((r.total - 1) / pageSize) + 1;
                    count = parseInt(r.total);
                    _this.attr("pageCount", pageCount);
                    _this.find("label").text("共有" + count + "条记录, " + "当前第" + pageIndex + "/" + pageCount + "页");
                    //调用回调函数
                    callback(r.data);
                }, error: function (xmlhttp) {
                    alert("错误：" + xmlhttp.responseText);
                }
            });
        }
        //进行首次请求
        reloadList();

        //为四个按钮编写单击事件
        _this.find(":image:eq(0)").click(function () {
            _this.attr("pageIndex", 1);
            reloadList();
        });
        _this.find(":image:eq(1)").click(function () {
            var pageIndex = _this.attr("pageIndex");
            pageIndex--;
            _this.attr("pageIndex", pageIndex);
            reloadList();
        });
        _this.find(":image:eq(2)").click(function () {
            var pageIndex = _this.attr("pageIndex");
            pageIndex++;
            _this.attr("pageIndex", pageIndex);
            reloadList();
        });
        _this.find(":image:eq(3)").click(function () {
            var pageCount = _this.attr("pageCount");
            _this.attr("pageIndex", pageCount);
            reloadList();
        });
    },
    "pager3": function (url, args, callback) {
        //添加四个按钮，以及一个label标签
        this.css("text-align", "right").css("padding-right", "30px");
        this.append(' <label>共有120条记录，当前第1/10页&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>')
            .append(' <input type="image" value="首页" src="../images/first.gif" />&nbsp;&nbsp;')
            .append(' <input type="image" value="上一页" src="../images/back.gif" />&nbsp;&nbsp;')
            .append(' <input type="image" value="下一页" src="../images/next.gif" />&nbsp;&nbsp;')
            .append(' <input type="image" value="尾页" src="../images/last.gif" />&nbsp;&nbsp;')
            .append(' 转到第<input type="text" size="1" /><input type="image" src="../images/go.gif" onclick=tiao(); />')
          
        //初始化三个本插件中的全局变量
        var _this = this;
        this.attr("pageIndex", 1);
        this.attr("pageSize", 3);
        this.attr("pageCount", 1);

        //重新加载数据(私有函数)
        var reloadList = function () {
            //得到三个参数
            var pageIndex = _this.attr("pageIndex");
            var pageSize = _this.attr("pageSize");
            var pageCount = _this.attr("pageCount");
            //修正页码
            if (parseInt(pageIndex) < 1)
                pageIndex = 1;
            if (parseInt(pageIndex) > parseInt(pageCount))
                pageIndex = pageCount;
            //对参数进行处理
            args = args || {};
            args.pageIndex = pageIndex;
            args.pageSize = pageSize;
            _this.attr("pageIndex", pageIndex);
            _this.attr("pageSize", pageSize);
            //请求数据
            $.get(url, args, function (result) {
                //将返回的结果转为js对象
                var r = JSON.parse(result);
                //求出总页数并显示
                pageCount = parseInt((r.total - 1) / pageSize) + 1;
                count = parseInt(r.total);
                _this.attr("pageCount", pageCount);
                _this.find("label").text("共有" + count + "条记录, " + "当前第" + pageIndex + "/" + pageCount + "页");
                //调用回调函数
                callback(r.data);
            });
        }
        //进行首次请求
        reloadList();

        //为四个按钮编写单击事件
        _this.find(":image:eq(0)").click(function () {
            _this.attr("pageIndex", 1);
            reloadList();
        });
        _this.find(":image:eq(1)").click(function () {
            var pageIndex = _this.attr("pageIndex");
            pageIndex--;
            _this.attr("pageIndex", pageIndex);
            reloadList();
        });
        _this.find(":image:eq(2)").click(function () {
            var pageIndex = _this.attr("pageIndex");
            pageIndex++;
            _this.attr("pageIndex", pageIndex);
            reloadList();
        });
        _this.find(":image:eq(3)").click(function () {
            var pageCount = _this.attr("pageCount");
            _this.attr("pageIndex", pageCount);
            reloadList();
        });
    }

});

